﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;

namespace WebServiceTMS.Helpers
{
    public static class StringGenerator
    {
        public static string GenerateString()
        {
            const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(chars, 24)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
            return result;
        }

        public static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input.
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            return false;
        }

        public static string GetMd5Hash(MD5 md5Hash, string input)
        {
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        public static int GetPriorityValue(string priority)
        {
            switch (priority)
            {
                case "High":
                    return 1;
                case "Middle":
                    return 2;
                case "Low":
                    return 3;
            }
            return 0;
        }

        public static string GetPriorityString(int value)
        {
            switch (value)
            {
                case 1:
                    return "High";
                case 2:
                    return "Middle";
                case 3:
                    return "Low";
            }
            return null;
        }

        public static Dictionary<string, string> GetDictionary(HttpRequestMessage request)
        {
            var content = request.Content;
            var jsonContent = content.ReadAsStringAsync().Result;
            var userObject = JObject.Parse(jsonContent);
            var tmp = new Dictionary<string, string>();
            foreach (KeyValuePair<String, JToken> resp in userObject)
            {
                tmp.Add(resp.Key, resp.Value.ToString());
            }
            return tmp;
        }
    }
}